/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.dao.impl;

import com.jspx.txweb.table.TreeItem;
import com.jspx.utils.StringUtil;
import com.jspx.utils.ArrayUtil;
import com.jspx.utils.BooleanUtil;

/**
 * Created by IntelliJ IDEA.
 * User:chenYuan (mail:39793751@qq.com)
 * Date: 2007-7-10
 * Time: 23:08:58
 *   Dtree 树的结构生成DAO
 */
public class DTreeItemDAOImpl extends TreeItemDAOImpl
{
    public DTreeItemDAOImpl()
    {
        
    }

    private String getItemJs(TreeItem node, boolean show, String selecteds)
    {
        if (node == null) return StringUtil.empty;
        if (!show && node.getHide() == 1) return StringUtil.empty;
        String[] selectedArray = StringUtil.split(selecteds,"/");
        StringBuilder result = new StringBuilder();
        result.append("<item text=\"").append(node.getCaption()).append("\" id=\"").append(node.getNodeId()).append("\" open=\"1\" select=\"").append(BooleanUtil.toInt(ArrayUtil.inArray(selectedArray, node.getNodeId(), true))).append("\">\r\n");
        for (TreeItem nNode : getChildTreeItem(node.getNodeId()))
        {
            if (checked == null || checked.length < 1)
            {
                result.append(getItemJs(nNode, show,  selecteds));
            } else if (ArrayUtil.inArray(checked,nNode.getNodeId(),true))
            {
                result.append(getItemJs(nNode, show,  selecteds));
            }
        }
        result.append("</item>\r\n");
        return result.toString();
    }
    

    public String getChildSrc(String nodeId)
    {
        TreeItem rootNode = getTreeItem(nodeId);
        if (rootNode == null) rootNode = getRootTreeItem();
        StringBuilder result = new StringBuilder("<?xml version='1.0' encoding='UTF-8'?>\r\n");
        result.append("<tree id=\"").append(rootNode.getNodeId()).append("\">\r\n");
        for (TreeItem nNode : getChildTreeItem(rootNode.getNodeId()))
        {
            if (checked == null || checked.length < 1)
            {
                result.append(getItemJs(nNode, show, selected));
            } else if (ArrayUtil.inArray(checked,nNode.getNodeId(),true))
            {
                result.append(getItemJs(nNode, show, selected));
            }
        }
        result.append("</tree>\r\n");
        return result.toString();
    }


    public String getTreeSrc()
    {
        TreeItem rootNode = getRootTreeItem();
        if (rootNode == null)
        {
            rootNode = createRootTreeItem();
        }
        
        StringBuilder result = new StringBuilder("<?xml version='1.0' encoding='UTF-8'?>\r\n");
        result.append("<tree id=\"").append(rootNode.getNodeId()).append("\">\r\n");
        for (TreeItem nNode : getChildTreeItem(rootNode.getNodeId()))
        {
            if (checked == null || checked.length < 1)
            {
                result.append(getItemJs(nNode, show, selected));
            } else if (ArrayUtil.inArray(checked,nNode.getNodeId(),true))
            {
                result.append(getItemJs(nNode, show, selected));
            }
        }
        result.append("</tree>\r\n");
        return result.toString();

    }
    
}